Page({
  data: {
    delBtnWidth:160,
    kefu: 1,
    list:  [{
      id:"18727508188",
      name:"钟婧",
      doctorTel:'',
      doctorName:"徐静茹",
      messageText:"医生您好",
      messageTime:"2020-5-1 14:30",
      messageUrl:"/images/defaultImage.png",
      type:0,
      isRead:1,
    },
    {
      id:"18727508188",
      name:"李雨辰李雨辰李雨辰李雨辰李雨辰",
      doctorTel:'',
      doctorName:"徐静茹",
      messageText:"赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看",
      messageTime:"2020-5-1 14:30",
      messageUrl:"/images/defaultImage.png",
      type:1,
      isRead:1,
    },
    {
      id:"18727508188",
      name:"张峻宇",
      doctorTel:'',
      doctorName:"徐静茹徐静茹徐静茹徐静茹徐静茹徐静茹",
      messageText:"赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看赶紧回家看",
      messageTime:"2020-5-1 14:30",
      messageUrl:"/images/defaultImage.png",
      type:0,
      isRead:0,
    },  
    ],
    isScroll:true,
    windowHeight:0,
    timer: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(e){
    var that = this
    this.enterPage(10);
    console.log(10)
    that.getMessage();
    that.data.timer = setInterval(function () {
      that.getMessage();
      console.log("轮播请求5秒触发一次");
    }, 5000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this
    //清除计时器  即清除setInter
    clearInterval(that.data.timer)
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    //清除计时器  即清除setIntertimer
    clearInterval(that.data.timer)
  },
  
  /**
   * 进入医生端 consultList页面，通知后端将进入此页面次数加一
   */
  enterPage: function (e) {
    var that = this
    wx.request({
      url: getApp().globalData.url + 'patientsettablebar',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        unitphone: getApp().globalData.username,
        pageNum: e
      },
    })
  },

  /**
   * 获取聊天列表
   */
  getMessage:function(e){
    var that = this;
    wx.request({
      url: getApp().globalData.url+'showCustomerServiceNum',
      method: 'GET',
      data: {
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {  
        if(res.data != 'False' && res.data != false)
        {
          that.setData({
            kefu: res.data.isRead
          })
        }  
      },
      fail(res){
        wx.showToast({
          title: '获取客服信息失败，请重新尝试',
          icon:'none'
        })
      }
    })
    wx.request({
      url:  getApp().globalData.url+'assistantShowMessage', // 仅为示例，并非真实的接口地址
      method: 'GET',
      data: {
        assistantTel: getApp().globalData.username
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {  
        console.log(res.data)
        if(res.data != "空" && res.data != "Flase" && res.data != "False")
        {
          that.setData({
            list: res.data
          })
        }  
        else
        {
          that.setData({
            list: null
          })
        }
      },
      fail(res){
        wx.showToast({
          title: '网络连接失败，请重新尝试',
          icon:'none'
        })
      }
    })
  },

  drawStart: function (e) {
    // console.log("drawStart");  
    var touch = e.touches[0]
    for(var index in this.data.list) {
      var item = this.data.list[index]
      item.right = 0
    }
    this.setData({
      list: this.data.list,
      startX: touch.clientX,
    })
 
  },
  drawMove: function (e) {
    var touch = e.touches[0]
    var item = this.data.list[e.currentTarget.dataset.index]
    var disX = this.data.startX - touch.clientX
    if (disX >= 20) {
      if (disX > this.data.delBtnWidth) {
        disX = this.data.delBtnWidth
      }
      item.right = disX
      this.setData({
        isScroll: false,
        list: this.data.list
      })
    } else {
      item.right = 0
      this.setData({
        isScroll: true,
        list: this.data.list
      })
    }
  },  
  drawEnd: function (e) {
    var item = this.data.list[e.currentTarget.dataset.index]
    if (item.right >= this.data.delBtnWidth/2) {
      item.right = this.data.delBtnWidth
      this.setData({
        isScroll: true,
        list: this.data.list,
      })
    } else {
      item.right = 0
      this.setData({
        isScroll: true,
        list: this.data.list,
      })
    }
  },
  
  /**
   * 跳转到聊天页面
   */
  toRecords: function(e) {
    var that = this
    var index = JSON.stringify(e.currentTarget.dataset.id)
    var other = that.data.list[index]
    var otherTel = JSON.stringify(that.data.list[index].id)
    var otherName = JSON.stringify(that.data.list[index].name)
    var doctorTel = JSON.stringify(that.data.list[index].doctorTel)
    var T = JSON.stringify(true)
    var F = JSON.stringify(false)
    if(other.doctorTel == "null")
    {
      wx.navigateTo({
        url: '../onlineConsulting/onlineConsulting?otherTel=' + otherTel + '&&otherName=' + otherName + '&&kefu=' + F + '&&doctorTel=' + otherTel + '&&isDoctor=' + T,
     })
    }
    else
    {
      wx.navigateTo({
        url: '../onlineConsulting/onlineConsulting?otherTel=' + otherTel + '&&otherName=' + otherName + '&&kefu=' + F + '&&doctorTel=' + doctorTel + '&&isDoctor=' + F,
     })
    }
  },

  /**
   * 跳转到在线客服列表
   */
  toCusService: function(e) {
    wx.navigateTo({
      url: '../cusServiceConsulting/cusServiceConsulting',
   })
  },

  /**
   * 删除聊天
   */
  delItem: function (e) {
    var that = this
    var index = JSON.stringify(e.currentTarget.dataset.id)
    var other = that.data.list[index]
    var otherTel = JSON.stringify(that.data.list[index].id)
    var doctorTel = JSON.stringify(that.data.list[index].doctorTel)
    if(other.doctorTel == null)
    {
      wx.request({
        url:  getApp().globalData.url + 'assistantDeleteChat',
        method: 'POST',
        data: {
          tel: getApp().globalData.username,
          doctorTel: otherTel,
          otherTel: null
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'  // 默认值
        },
        success(res){
          wx.showToast({
            title: '成功删除聊天记录',
            icon: 'none',
          })  
          that.getMessage() 
        },
        fail(res){
          wx.showToast({
            title: '删除聊天记录失败',
            icon: 'none',
        })   
        }
      })
    }
    else
    {
      wx.request({
        url:  getApp().globalData.url + 'assistantDeleteChat',
        method: 'POST',
        data: {
          tel: getApp().globalData.username,
          doctorTel: doctorTel,
          otherTel: otherTel
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'  // 默认值
        },
        success(res){
          wx.showToast({
            title: '成功删除聊天记录',
            icon: 'none',
          })   
          that.getMessage()
        },
        fail(res){
          wx.showToast({
            title: '删除聊天记录失败',
            icon: 'none',
        })   
        }
      })
    }
  }
})